import java.util.Scanner;

class NimPlayer {
    private final String name;
    private int move_count;

    public NimPlayer(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setMove_count(int move_count) {
        this.move_count = move_count;
    }

    public int removeStone() {
        return move_count;
    }
}

public class Main {

    //打印石头个数
    static void print_stone(int remain) {
        if (remain > 1)
            System.out.print(remain + " stones left:");
        else
            System.out.print(remain + " stone left:");
        for (int i = 0; i < remain; i++) {
            System.out.print(" *");
        }
        System.out.println();
    }

    //判断胜负
    static void judge(int remain, NimPlayer obj) {
        if (remain == 0) {
            System.out.println("Game Over");
            System.out.println(obj.getName() + " wins!\n");
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String select;
        do {
            System.out.println("Welcome to Nim\n");
            System.out.println("Please enter Player 1's name:");
            String name1 = sc.next();
            NimPlayer p1 = new NimPlayer(name1);
            System.out.println(p1.getName() + "\n");

            System.out.println("Please enter Player 2's name:");
            String name2 = sc.next();
            NimPlayer p2 = new NimPlayer(name2);
            System.out.println(p2.getName() + "\n");

            System.out.println("Please enter upper bound of stone removal:");
            int upper = sc.nextInt();
            System.out.println(upper + "\n");
            System.out.println("Please enter initial number of stones:");
            int initial = sc.nextInt();
            System.out.println(initial + "\n");

            print_stone(initial);

            while (initial > 0) {
                System.out.println(p1.getName() + "’s turn - remove how many?");
                p1.setMove_count(sc.nextInt());
                System.out.println(p1.removeStone() + "\n");
                initial -= p1.removeStone();
                judge(initial, p2);
                if (initial > 0)
                    print_stone(initial);
                else
                    break;

                System.out.println(p2.getName() + "’s turn - remove how many?");
                p2.setMove_count(sc.nextInt());
                System.out.println(p2.removeStone() + "\n");
                initial -= p2.removeStone();
                judge(initial, p1);
                if (initial > 0)
                    print_stone(initial);
                else
                    break;
            }

            System.out.print("Do you want to play again (Y/N):");
            select = sc.next();
            System.out.print(select);
        } while (select.equals("Y"));
    }
}
